<?php



namespace app\common\model;


use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Request;
use think\facade\Session;
/**
 * 有关时间的模型
 * Class TimeModel
 * @package app\common\model
 */
class TimeModel extends Model
{

    public static $app_id;

    public static $is_super;
    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    protected $store_id;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    protected static $admin;
    /**
     * 软删除
     */
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    protected $globalScope = ['app_id'];

    /**
     * 模型基类初始化
     */
    public static function init()
    {
        parent::init();
        // 后期静态绑定app_id
        //self::$app_id = Request::param('app_id');
        //if(!self::$app_id)
        self::checkApp();

        //self::bindAppId();
        self::$admin = Session::get('admin');
    }
    /**
     * 验证项目app
     */
    public static function checkAPP(){
        $user = Session::get('admin');
        if($user['id']){
            self::$app_id = !empty($user['is_super'])? 0 :$user['app_id'];
        }else{

            self::$app_id = Request::param('app_id',10001);
        }
    }

    /**
     * 获取app
     * @return mixed
     */
    public function getApp(){
        $self = new static();
        if(!empty(Request::param('app_id'))){
            $this->app_id = Request::param('app_id');
        }else{
            $this->app_id = $self::$app_id;
        }


        return $this->app_id;
    }

    /**
     * 定义全局的查询范围
     * @param \think\db\Query $query
     */
    public function scopeApp_id($query)
    {

        if (!empty(self::$app_id)) { //
            $query->where('app_id', self::$app_id);
        }else{

        }

    }
    /**
     * 全局获取接口app_id
     * @return mixed
     */
    public static function get_appId()
    {
        return Request::param('app_id',10001);

    }

    /**
     * 全局获取商家id
     * @return mixed
     */
    public function get_store()
    {
        $user =  Session::get('admin');
        $this->store_id = $user['store_id'];
        return $this->store_id;
    }


}